package top.linuyx.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * @Author Linuyx
 * @Description Druid配置类
 * @Date Created in 2021-04-07 22:20
 */

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    //后台监控
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(
                new StatViewServlet(), "/druid/*");

        HashMap<String,String> initParameters = new HashMap<>();
        //账号密码
        initParameters.put("loginUsername","linuyx");
        initParameters.put("loginPassword","3bf24a76");

        //允许谁能访问
        initParameters.put("allow","");//为空字符串,所有人访问;不为空字符串，特定的人能访问

        //禁止谁访问
        //initParameters.put("deny","127.0.0.1");

        bean.setInitParameters(initParameters);

        return bean;
    }

}
